package com.example.pickerdemo.slice;

import com.example.pickerdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Picker;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取Picker组件实例
        Picker picker = (Picker) findComponentById(ResourceTable.Id_picker);
        // 设置可选择的最小值
        picker.setMinValue(0);
        // 设置可选择的最大值
        picker.setMaxValue(10);
        // 设置默认状态下的文字大小
        picker.setNormalTextSize(60);
        // 设置选中状态下的文字大小
        picker.setSelectedTextSize(60);
        // 设置选中状态下的文字颜色
        picker.setSelectedTextColor(Color.BLACK);
        // 选中值变化时的监听
        picker.setValueChangedListener((component, oldValue, newValue) -> {
            ToastDialog dialog = new ToastDialog(getContext());
            // oldValue为变化之前的值，newValue为变化之后的值
            dialog.setText(String.format("v1:%d, v2:%d", oldValue, newValue));
            dialog.show();
        });
        // 设置可选值为 0， 1， 2
        picker.setMinValue(0);
        picker.setMaxValue(2);
        // 将可选值对应的映射到格式化后的字符串
        picker.setFormatter(new Picker.Formatter() {
            @Override
            public String format(int i) {
                if (i == 0) {
                    return "男";
                } else if (i == 1) {
                    return "女";
                } else {
                    return "保密";
                }
            }
        });

        picker.setMinValue(0);
        picker.setMaxValue(4);
        picker.setDisplayedData(new String[]{"10°C", "11°C", "12°C", "13°C", "14°C"});

        picker.setSelectorItemNum(3);
        picker.setSelectedNormalTextMarginRatio(20);
        picker.setWheelModeEnabled(true);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
